package com.vma.jifu.controller;


import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;

import com.vma.jifu.bizModel.model.OrderList;
import com.vma.jifu.orderList.service.OrderService;
import com.vma.jifu.orderList.service.impl.OrderServiceImpl;
import com.vma.jifu.service.AgentService;
import com.vma.jifu.service.impl.AgentServiceImpl;
import com.vma.tools.PayResult;
import com.vma.tools.ReturnsUtil;
import com.vma.tools.XMLUtils;
import com.vma.tools.wxPay.WxPay;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;


public class RecvWeiXinParamController  extends Controller {


	private static final Logger LOG = LoggerFactory.getLogger(RecvWeiXinParamController.class);

	private AgentService agentService = new AgentServiceImpl();

	private OrderService orderService  = new OrderServiceImpl();
	/**
	 * 
	 * <xml><appid><![CDATA[wxf380b30bb430d138]]></appid><bank_type><![CDATA[CFT]]></bank_type><cash_fee><![CDATA[1]]></cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[Y]]></is_subscribe><mch_id><![CDATA[1240459502]]></mch_id><nonce_str><![CDATA[aZohipttHmAGpFTL1PmjvidEuq0jmNFz]]></nonce_str><openid><![CDATA[orS1LuKbo6nQW9S6cPvu9poDwogA]]></openid><out_trade_no><![CDATA[2015050813503167758]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[7325103D2A349CC89C3EE40E6063A7F8]]></sign><time_end><![CDATA[20150508135041]]></time_end><total_fee>1</total_fee><trade_type><![CDATA[JSAPI]]></trade_type><transaction_id><![CDATA[1009070562201505080116467741]]></transaction_id></xml>
		<xml>
		<appid><![CDATA[wxf380b30bb430d138]]></appid>
		<bank_type><![CDATA[CFT]]></bank_type>
		<cash_fee><![CDATA[1]]></cash_fee>
		<fee_type><![CDATA[CNY]]></fee_type>
		<is_subscribe><![CDATA[Y]]></is_subscribe>
		<mch_id><![CDATA[1240459502]]></mch_id>
		<nonce_str><![CDATA[aZohipttHmAGpFTL1PmjvidEuq0jmNFz]]></nonce_str>
		<openid><![CDATA[orS1LuKbo6nQW9S6cPvu9poDwogA]]></openid>
		<out_trade_no><![CDATA[2015050813503167758]]></out_trade_no>
		<result_code><![CDATA[SUCCESS]]></result_code>
		<return_code><![CDATA[SUCCESS]]></return_code>
		<sign><![CDATA[7325103D2A349CC89C3EE40E6063A7F8]]></sign>
		<time_end><![CDATA[20150508135041]]></time_end>
		<total_fee>1</total_fee>
		<trade_type><![CDATA[JSAPI]]></trade_type>
		<transaction_id><![CDATA[1009070562201505080116467741]]></transaction_id>
		</xml>
		
		30e351e70f31d74acaa6e611eb11cd8c

	 * @throws IOException 

	 */
	@Clear
	public void wxPayListener() throws IOException{
		 BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)getRequest().getInputStream()));
		 String line = null;
         StringBuilder sb = new StringBuilder();
         while((line = br.readLine())!=null){
             sb.append(line);
         }
         LOG.debug("wxPayListener==========="+sb.toString());
		 PayResult payResult= XMLUtils.getPayResult(sb.toString());
		 String olNbr = payResult.getOutTradeNo();
		 System.out.println(getRequest().getRemoteHost());
		 if(orderService.callBackForOrder(olNbr,2)){

		 }else {
			 agentService.callBackForAgentCharge(olNbr,2);
		 }
		 getResponse().getWriter().write(" <xml> <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
	}

	public void unifiedorder() throws Exception{

		String orderNbr = getPara("orderNbr");
		OrderList orderList = OrderList.dao.findFirst("SELECT * FROM order_list WHERE order_nbr = ?",orderNbr);
		Map map = WxPay.payInfo(orderList.getOrderNbr(),"能库",orderList.getTotalFee()+"","http://p.newjihui.com/interface_war_exploded/wxinPay/wxPayListener");
		System.out.println("起调接口返回："+map);
		renderJson(ReturnsUtil.returnSuccess(WxPay.wxPay(map)));

	}

	@Clear
	public void wxPayListenerAgentCharge()throws IOException{
		BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)getRequest().getInputStream()));
		String line = null;
		StringBuilder sb = new StringBuilder();
		while((line = br.readLine())!=null){
			sb.append(line);
		}
		LOG.debug("wxPayListener==========="+sb.toString());
		PayResult payResult= XMLUtils.getPayResult(sb.toString());
		String olNbr = payResult.getOutTradeNo();
		System.out.println(getRequest().getRemoteHost());
		if(agentService.callBackForAgentCharge(olNbr,Integer.valueOf(payResult.getTotalFee()))){
			getResponse().getWriter().write(" <xml> <return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>");
		}

	}

}
